package jmine.tec.utils.reflection.fast.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import jmine.tec.utils.reflection.fast.AbstractFastMethod;
import jmine.tec.utils.reflection.fast.FastMethod;
import jmine.tec.utils.reflection.fast.FastMethodNoArgs;
import jmine.tec.utils.reflection.fast.FastMethodSingleArgument;

/**
 * Implementacao trivial de {@link FastMethod}
 * 
 * @author takeshi
 */
public class FastMethodImpl extends AbstractFastMethod implements FastMethod, FastMethodNoArgs, FastMethodSingleArgument {

    /**
     * C'tor
     * 
     * @param method o {@link Method}
     */
    public FastMethodImpl(Method method) {
        super(method);
    }

    /**
     * {@inheritDoc}
     * 
     * @throws Exception
     */
    public Object invokeNoArgs(Object target) throws Exception {
        return this.invoke(target);
    }

    /**
     * {@inheritDoc}
     */
    public Object invokeSingleArgument(Object target, Object arg) throws Exception {
        return this.invoke(target, arg);
    }

    /**
     * {@inheritDoc}
     */
    public Object invoke(Object target, Object... args) throws Exception {
        try {
            return this.getMethod().invoke(target, args);
        } catch (InvocationTargetException e) {
            final Throwable te = e.getTargetException();
            if (te instanceof Error) {
                throw (Error) te;
            }
            throw (Exception) te;
        }
    }

}
